<?php

class AdminhtmlHelper
{
	public static function getMenuArray()
	{
		//url,label,icon,disabled,visible,
		//url: router,link,params,htmlOptions
		$array = array( array('url'=>array( 'route'=>'/adminhtml/privilege/index', ), 'label'=>'Home',
			array('url'=>array( 'route'=>'/site/index','htmlOptions'=> array( 'target'=> '_blank') ), 'label'=>'前台',),
			array('url'=>array( 'route'=>'/adminhtml/privilege/profile', ), 'label'=>'我的帐户',),
		
		));
		$nodeModel = AdminhtmlNode::model();
		$tree= $nodeModel->getNodeTree(/*$level=1*/);
		//print_r($tree);die;
		foreach ($tree as $v) {
			array_push($array, $v);
		}
		//array_push($array,  );
		if( !Yii::app()->admin->getIsGuest() ){
			array_push($array, array('url'=>array( 'route'=>'/adminhtml/privilege/logout', ), 'label'=>'退出（'. Yii::app()->admin->name . '）',) );
		}
		$menuArray['menu']= $array;
		$menuArray['stylesheet']= "menu_white.css";
		$menuArray['menuID']= "adminhmlt_top_menu";
		$menuArray['delay']= 3;
		
		return $menuArray;
	}
	
	public static function getStatusOptions()
	{
		return array('0'=>' 禁用 ','1'=>' 激活 ');
	}
	public static function getStatusLabel($value=null)
	{
		$array = self::getStatusOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	public static function getNodeOptions()
	{
		$nodeModel= AdminhtmlNode::model();
		$options= $nodeModel->getNodeOptionsHash();
		//print_r($options);die;
		$options= array(0=> '- 顶级分类 -')+ $options;
		return $options;
	}
	
	public static function getControllerOptions($except=true, $exceptControllers=null)
	{
		//echo Yii::app()->basePath; => ../protected
		$dir = Yii::app()->basePath. '/modules/adminhtml/controllers/';
		$dir_handle= opendir($dir);
		$array= array();
	    if($dir_handle ) {
	        while( ($file= readdir($dir_handle))!==false ) {
	            if($file==='.' || $file==='..' ) {
	                continue;
	            }
	            $tmp= realpath($dir. '/'. $file);
	            if( !is_dir($tmp) ) {
	            	$tmp = substr(strstr($tmp, 'controllers'), 12, -4);
	                $array[$tmp]= $tmp;
	            }
	        }
	        closedir($dir_handle);  
	    }
	    if ( $except===true) {
		    $exceptControllers = array_merge((array) $exceptControllers, AdminhtmlNode::getExceptionControllers());
		    foreach ($exceptControllers as $v){
		    	unset($array[$v]);
		    }
	    }
	    return $array;
	}
	
	public static function getRoleOptions($exceptId=null)
	{
		$model= AdminhtmlRole::model();
		$options= $model->toOptionHash();
		//print_r($options);die;
		if ($exceptId) {
			unset($options[$exceptId]);
		}
		return $options;
	}
	
	/**
	 * return role tree html
	 * @param Array $selected false|ALL_PRIVILEGES|array()
	 */
	public static function getRoleTreeHtml( $selected=false )
	{
		if ( $selected!==false && $selected!==Yii::app()->params['fullAccess'] ) {
			$selected=  @unserialize($selected);
		}
		$nodeModel = new AdminhtmlRole();
		$array= $nodeModel->getRoleTreeArray();
		//print_r($array);die;
		$html= '';
		$checkHtml= ' checked=true ';
		foreach ($array as $k=> $v) {
		    $html.= "\n<li><input id='{$k}' value='{$k}' type='checkbox'> <b>". $v['label']. "</b> [{$k}]\n<ul>";
			foreach ($v['actions'] as $sk=> $sv){
				$html.= "\n<li>";
				if ( isset($selected[$k]) && is_array($selected[$k]) && in_array(strtolower($sv), $selected[$k] ) ) {
					$html.= '<input id="'. $k. '['.$sv . ']" name="'. $k. '__'.$sv . '" value="'.$sv .'"'. $checkHtml. 'type="checkbox"> '. $sv ;
				} else {
					$html.= '<input id="'. $k. '['.$sv . ']" name="'. $k. '__'.$sv . '" value="'.$sv .'" type="checkbox"> '. $sv ;
				}
				$html.= "\n</li>";
			}
			$html.= "</ul>\n";
		}
		//echo $html;die;
		return $html;
	}
	
	public static function getLogTypeOptions()
	{
		$model= AdminhtmlLog::model();
		$types= $model->getActionType();
		return $types;
	}
	public static function getLogTypeLabel($value=null)
	{
		$array = self::getLogTypeOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	//=============================================================
	//Recommender reference
	
	public static function getRecommenderOptions($exceptId=null)
	{
		$model= Recommender::model();
		$options= $model->toOptionHash();
		//print_r($options);die;
		if ($exceptId) {
			unset($options[$exceptId]);
		}
		return $options;
	}
	public static function getRecommenderLabel($value=null)
	{
		$array = self::getRecommenderOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	public static function getRecommenderTypeOptions()
	{
		$model= Recommender::model();
		$options= $model->getTypeHash();
		return $options;
	}
	public static function getRecommenderTypeLabel($value=null)
	{
		$array = self::getRecommenderTypeOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	public static function getRecommenderBankOptions()
	{
		$model= RecommenderBank::model();
		$options= $model->toOptionHash();
		return $options;
	}
	public static function getRecommenderBankLabel($value=null)
	{
		$array = self::getRecommenderBankOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	public static function getBanksOptions()
	{
		$model= RecommenderBank::model();
		$options= $model->getBanksOptions();
		return $options;
	}
	public static function getBanksLabel($value=null)
	{
		$array = self::getBanksOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	public static function getProvinceOptions()
	{
		return FrontHelper::getProvinceOptions();
	}
	
	public static function getCityOptions($provinceId)
	{
		return FrontHelper::getCityOptions($provinceId);
	}
	
	public static function getPostcodeFromCity($city)
	{
		return FrontHelper::getPostcodeFromCity($city);
	}
	
	
	
	//=============================================================
	//Article reference
	
	public static function getArticleCatOptions()
	{
		$nodeModel= ArticleCat::model();
		$options= $nodeModel->getCatOptionsHash();
		//print_r($options);die;
		$options= array(0=> '- 顶级分类 -')+ $options;
		return $options;
	}
	
	public static function getResourceOptions()
	{
		$model= Resource::model();
		$options= $model->getTypeOptions();
		return $options;
	}
	public static function getResourceLabel($value=null)
	{
		$array = self::getResourceOptions();
		if ($value===null ) {
			return '-';
		} else {
			return $array[$value];
		}
	}
	
	
	
}